﻿using System.Windows;
using System.Windows.Media;
using System.ComponentModel;
using System;
using System.Windows.Controls;
using System.Windows.Input;

namespace ImageProcessing.ParameterForms
{
    /// <summary>
    /// Логика взаимодействия для FormSubImageConstants.xaml
    /// </summary>
    public partial class FormSubImageConstants : Window, INotifyPropertyChanged
    {
        public FormSubImageConstants()
        { InitializeComponent(); }

        private ImageSource _SourceImage;
        public ImageSource SourceImage
        {
            get { return _SourceImage; }
            set
            {
                _SourceImage = value;
                OnPropertyChanged("SourceImage");
            }
        }

        private void Button_OK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void Button_Cancel_Click(object sender, RoutedEventArgs e)
        { Close(); }

        private Point SelectionStartPoint = new Point();

        private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SelectionStartPoint = e.GetPosition(canvas);
            Canvas.SetZIndex(selection, canvas.Children.Count);
            if (!canvas.IsMouseCaptured) canvas.CaptureMouse();
            canvas.Cursor = Cursors.Cross;
        }

        private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (canvas.IsMouseCaptured) canvas.ReleaseMouseCapture();
            canvas.Cursor = Cursors.Arrow;
        }

        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (canvas.IsMouseCaptured)
            {
                Point currentPoint = e.GetPosition(canvas);
                double x = SelectionStartPoint.X < currentPoint.X ? SelectionStartPoint.X : currentPoint.X;
                double y = SelectionStartPoint.Y < currentPoint.Y ? SelectionStartPoint.Y : currentPoint.Y;
                double width = currentPoint.X - SelectionStartPoint.X;
                double height = currentPoint.Y - SelectionStartPoint.Y;
                if (e.GetPosition(image).X < 0)
                {
                    x = 0;
                    width = SelectionStartPoint.X;
                }
                if (e.GetPosition(image).Y < 0)
                {
                    y = 0;
                    height = SelectionStartPoint.Y;
                }
                if (e.GetPosition(image).X > canvas.ActualWidth)
                    width = canvas.ActualWidth - SelectionStartPoint.X;
                if (e.GetPosition(image).Y > canvas.ActualHeight)
                    height = canvas.ActualHeight - SelectionStartPoint.Y;

                if (selection.Visibility == Visibility.Hidden) selection.Visibility = Visibility.Visible;

                selection.RenderTransform = new TranslateTransform(x, y);
                selection.Width = Math.Abs(width);
                selection.Height = Math.Abs(height);
            }
        }

        #region INotifyPropertyChanged parts
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}